GBML manual / Backgrounds

draw_background

Shows bg for this frame with its picture’s top-left corner at (x, y), counted from the room’s top-left corner. A background you made with background_create is hidden until a Draw event draws it, and it is hidden again in every frame it is not drawn. In a Draw GUI event it is fixed to the screen instead, and (x, y) counts from the screen’s top-left pixel.

Works differently on Game Boy - The Game Boy has one background layer, which the room's picture uses, so the picture is drawn where background_create put it and nowhere else.

Works differently on Game Boy Color - The Game Boy Color has one background layer, which the room's picture uses, so the picture is drawn where background_create put it and nowhere else.

On other consoles this differs: see Side by side and How the consoles differ.

Syntax

draw_background(bg, x, y)

Arguments

NameType
bgbackground
xreal
yreal

Returns

Nothing.

Description

Shows bg for this frame, with its picture's top-left corner at (x, y). A no-op on nothing.

This is how a background gets onto the screen. background_create loads the picture and starts it hidden, and nothing is on the screen until a Draw event calls draw_background with the handle. Like every draw_ call it only works in a Draw event, and a background you do not draw in a frame is hidden in that frame. To hide one, stop drawing it.

In a Draw event (Draw Begin, Draw or Draw End), (x, y) is a place in the room, counted from the room's top-left corner, and the camera carries the background along with everything else. In a Draw GUI event the same call is fixed to the screen instead: it ignores the camera and (x, y) counts from the screen's top-left pixel, so a menu backdrop or a dimming screen stays put while the camera moves.

The order you draw in is the order things stack

In one Draw event (Draw Begin, Draw, Draw End or Draw GUI), whatever you draw later is in front of whatever you drew earlier. That is true for sprites, text, rectangles and backgrounds together. Draw a sprite and then a big solid background, and the background hides the sprite. Draw the background first and then the sprite, and the sprite shows on top of it:

draw_sprite(spr_hero, 0, 100, 60);
draw_background(wall, 0, 0);      // a big solid picture: the hero is hidden behind it
draw_background(wall, 0, 0);
draw_sprite(spr_hero, 0, 100, 60); // the hero is drawn on top of the wall

In a Draw GUI event the background is also in front of the room's own pictures and the room's objects, and you can still move it every frame.

A layer you gave the background with background_priority is kept. On that layer a sprite still beats a background, and between two backgrounds on the same layer the one you draw later is in front. A background you never gave a layer to gets its layer from the order of your calls, as described next.

How it works, and how many switches fit

The console has only 4 layers (0 is the front, 3 is the back), and on one layer a sprite always beats a background. So each time you draw a background right after a sprite, the game moves that background, and everything you draw after it in the same event, one layer toward the front. Two backgrounds drawn one after the other, with no sprite between them, stay on the same layer and the later one is in front.

A Draw event starts on its object's own bg_priority (3 unless you changed it), so it has room for 3 switches (sprite, background, sprite, background, sprite, background). An object whose bg_priority is 1 has room for 1. A Draw GUI event counts back from the front layer, so it also has room for 3. When a Draw GUI event starts doing this, its first frame can show the background one frame late.

Every background you draw is also one of the console's showing layers, and the room uses some of them, so the number of switches is also held down by how many backgrounds the room leaves free. Every switch needs a background of its own:

The room hasBackgrounds you can draw at onceSwitches that fit
no tile layer43
1 tile layer33
2 tile layers22
3 tile layers11
a zoomable background and no tile layer22
a zoomable background and 1 tile layer11

A shrouded (dark) room uses one more, and so does an open dialogue box. If you ask for more switches than fit, the extra background is drawn behind the sprite that came right before it (the best the console can do), and a development build says so in its log.

The checker warns when a drawing event, written straight out, switches more than 3 times. The screen-fixed calls whose names end in _anchored always stay in front of everything, so they are not part of this order.

Each event starts over. What other objects draw is not part of your order: objects are still ordered by their groups and depth, and a sprite from another object on the same layer as your background is in front of it. An object lifted to layer 0 (to stay bright in a dark room) also stays in front of a Draw GUI background.

Cheap enough to call every frame: it writes the background's position and shows it, and nothing else. Never destroy and re-create a background just to move it. That churns the hardware's tile and map allocator once per frame and can fail partway through.

Only a background that is being drawn uses one of the 4 showing layers, and only in the frames it is drawn. If drawing one would put one layer too many on the screen (the room's own picture, its shroud and other backgrounds count), the call does nothing and the background stays hidden in that frame. A room with a zoomable background has only 2 ordinary layers.

Example

draw_background(backdrop, 0, 0);

Draws the backdrop made in this object's Create event with its top-left corner on the room's top-left corner, in every frame the Draw event runs. Scrolling the camera slides it along with the room. Put the same line in a Draw GUI event and it stays on the screen instead.

On the GBA

A no-op on `nothing`. Call it from a Draw (or Draw Begin / Draw End) event to put the background in the room: the shared camera carries it, so scrolling the camera carries the background along with everything else. Call it from a Draw GUI event to pin it to the screen: it ignores the camera and (x, y) counts from the screen’s top-left pixel, so a menu backdrop stays still while the camera moves. The layer comes from the ORDER of the calls in the event, unless you gave the background a layer with background_priority - then that layer is kept, and only the order of the calls decides which of two things on the same layer is in front. Whatever is drawn later is in front of what was drawn earlier, sprites and backgrounds together (a background drawn right after a sprite moves one layer toward the front, for itself and for everything drawn after it; a Draw GUI background is in front of the room and the room’s objects). An event has room for 3 such switches; past that the development log says so. Cheap enough to call every frame: it writes the background’s position and shows it, and nothing else - no memory is moved. A background that is not drawn in a frame is hidden for that frame, so to hide one, stop calling this. Only a background being drawn uses one of the 4 showing-at-once layers (2 in a room with a zoomable background), room and project combined; if drawing it would put one layer too many on the screen, this call does nothing and the background stays hidden that frame. Drawing is only allowed in Draw events, and the checker says so if you call this anywhere else.

See also: background_createbackground_destroybackground_prioritydraw_spritedraw_guiBackgrounds